package cloud.alibaba.study.config;

import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 手动加载限流规则(流控规则)
 * @ClassName:FlowRuleInitFunc   
 * @Description:TODO(描述这个类的作用)   
 * @author:Calvin.W
 * @date:2022年1月5日 下午2:53:48
 */
public class FlowRuleInitFunc implements InitFunc{
    @Override
    public void init() throws Exception {
        List<FlowRule> rules=new ArrayList<>();
        FlowRule rule=new FlowRule();
        rule.setCount(5);//限流阈值
        rule.setResource("helloSentinel");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);//限流阈值类型
        //QPS超过阈值时,流量控制行为
        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);//直接拒绝
       // rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_WARM_UP);//冷启动(预热)
       // rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);//匀速排队
       // rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER);//冷启动+匀速排队
        rule.setLimitApp("default");//是否需要针对调用来源进行限流
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}
